<?php

namespace Happy\CommandsService\Database\Models;

use Illuminate\Database\Eloquent\Model;

class SessionsModel extends Model
{
    public $table = 'sessions';

    public $timestamps = false;

    protected $primaryKey = 'id';

    protected $fillable = ['user_id', 'server', 'start_time', 'end_time', 'active', 'parameters'];

    protected function casts(): array
    {
        return [
            'server' => 'string',
        ];
    }

    public function close()
    {
        $this->active = false;
        $this->end_time = time();

        return $this;
    }

    public function commands()
    {
        return $this->hasMany(CommandsModel::class, 'session_id', 'id');
    }
}
